-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
31 lines (26 loc) · 880 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.*;
public class LargestNumber {
// Custom comparator
static class CustomComparator implements Comparator<String> {
@Override
public int compare(String x, String y) {
return (y + x).compareTo(x + y); // Sort in descending order
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
String[] nums = new String[n];
System.out.println("Enter the numbers:");
for (int i = 0; i < n; i++) {
nums[i] = scanner.next();
}
Arrays.sort(nums, new CustomComparator());
System.out.print("Largest number: ");
for (String num : nums) {
System.out.print(num);
}
System.out.println();
}
}